package person.aoao.library.controller;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import person.aoao.library.dao.SeatListDao;
import person.aoao.library.entity.SeatList;
import person.aoao.library.result.ResponseCode;
import person.aoao.library.result.ResponseData;
import person.aoao.library.service.SeatListService;

import javax.annotation.Resource;
import java.util.List;

/**
 * (SeatList)表控制层
 *
 * @author makejava
 * @since 2023-05-05 10:15:40
 */
@RestController
@CrossOrigin
@RequestMapping("seatList")
public class SeatListController {
    /**
     * 服务对象
     */
    @Resource
    private SeatListService seatListService;

    @Resource
    private SeatListDao seatListDao;
    /**
     * 分页查询
     *
     * @param seatList    筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<SeatList>> queryByPage(SeatList seatList, PageRequest pageRequest) {
        return ResponseEntity.ok(this.seatListService.queryByPage(seatList, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<SeatList> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.seatListService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param seatList 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<SeatList> add(SeatList seatList) {
        return ResponseEntity.ok(this.seatListService.insert(seatList));
    }

    /**
     * 编辑数据
     *
     * @param seatList 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<SeatList> edit(SeatList seatList) {
        return ResponseEntity.ok(this.seatListService.update(seatList));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.seatListService.deleteById(id));
    }

    /*
        通过楼层获取座位

     */
    @GetMapping("getSeatByFid/{fid}")
    public ResponseData getSeatByFid(@PathVariable Integer fid){
        List<SeatList> data=this.seatListService.getSeatList(fid);
        return new ResponseData(ResponseCode.SUCCESS,data);
    }

    @GetMapping("getSeatList/{num}/{page}")
    public ResponseData getSeatList(@PathVariable Integer num,@PathVariable Integer page){
        ResponseData data=this.seatListService.getSeatListAll(num,page);
        return  data;
    }


    @PostMapping("searchSeatList/{fid}/{state}/{seattype}/{num}/{page}")
    public ResponseData searchSeatList(@RequestBody String str,@PathVariable Integer fid,@PathVariable Integer state,@PathVariable Integer seattype,@PathVariable Integer num,@PathVariable Integer page){
        ResponseData data=this.seatListService.searchSeatList(str.replace("\"", ""),fid,state,seattype,num,page);
        return  data;
    }

    @PostMapping("addSeatList")
    public ResponseData addSeatList(@RequestBody SeatList seatList){
        seatList.setUsecount("0");
        return new ResponseData(ResponseCode.SUCCESS,this.seatListService.insert(seatList));
    }
    @PostMapping("updateSeatList")
    public ResponseData editSeatList(@RequestBody SeatList seatList){
        return new ResponseData(ResponseCode.SUCCESS,this.seatListService.update(seatList));
    }
    @PostMapping("deleteSeatList")
    public ResponseData deleteSeatList(@RequestBody SeatList seatList){
        return new ResponseData(ResponseCode.SUCCESS,this.seatListService.deleteById(seatList.getId()));
    }

}

